<template>
  <div class="table-page">
    <GiTable title="设备资源管理" row-key="id" :data="dataList" :columns="columns" :loading="loading"
      :scroll="{ x: '100%', y: '100%', minWidth: 1000 }" :pagination="pagination" :disabled-tools="['size']"
      :disabled-column-keys="['name']" @refresh="search">
      <template #toolbar-left>
        <a-input v-model="queryForm.name" placeholder="请输入设备名称" allow-clear @change="search">
          <template #prefix><icon-search /></template>
        </a-input>
        <a-select v-model="queryForm.category" :options="device_category" placeholder="请选择设备类别" allow-clear
          style="width: 150px" @change="search" />
        <a-input v-model="queryForm.modelNumber" placeholder="请输入型号" allow-clear @change="search">
          <template #prefix><icon-search /></template>
        </a-input>
        <a-input v-model="queryForm.factoryNumber" placeholder="请输入出厂编号" allow-clear @change="search">
          <template #prefix><icon-search /></template>
        </a-input>
        <a-date-picker v-model="queryForm.buyDate" placeholder="请选择购置时间" show-time format="YYYY-MM-DD HH:mm:ss"
          style="height: 32px" />
        <a-button @click="reset">
          <template #icon><icon-refresh /></template>
          <template #default>重置</template>
        </a-button>
      </template>
      <template #toolbar-right>
        <a-button v-permission="['task:deviceInfo:add']" type="primary" @click="onAdd">
          <template #icon><icon-plus /></template>
          <template #default>新增</template>
        </a-button>
        <a-button v-permission="['task:deviceInfo:export']" @click="onExport">
          <template #icon><icon-download /></template>
          <template #default>导出</template>
        </a-button>
      </template>
      <template #category="{ record }">
        <GiCellTag :value="record.category" :dict="device_category" />
      </template>
      <template #measureUnit="{ record }">
        <GiCellTag :value="record.measureUnit" :dict="measure_unit" />
      </template>
      <template #destination="{ record }">
        <GiCellTag :value="record.destination" :dict="destination" />
      </template>
      <template #action="{ record }">
        <a-space>
          <a-link v-permission="['task:deviceInfo:detail']" title="详情" @click="onDetail(record)">详情</a-link>
          <a-link v-permission="['task:deviceInfo:update']" title="修改" @click="onUpdate(record)">修改</a-link>
          <a-link v-permission="['task:deviceInfo:delete']" status="danger" :disabled="record.disabled"
            :title="record.disabled ? '不可删除' : '删除'" @click="onDelete(record)">
            删除
          </a-link>
        </a-space>
      </template>
    </GiTable>

    <DeviceInfoAddModal ref="DeviceInfoAddModalRef" @save-success="search" />
    <DeviceInfoDetailDrawer ref="DeviceInfoDetailDrawerRef" />
  </div>
</template>

<script setup lang="ts">
import DeviceInfoAddModal from './DeviceInfoAddModal.vue'
import DeviceInfoDetailDrawer from './DeviceInfoDetailDrawer.vue'
import { useDownload, useTable } from '@/hooks'
import { useDict } from '@/hooks/app'
import { isMobile } from '@/utils'
import has from '@/utils/has'
import {
  type DeviceInfoResp,
  type DeviceInfoQuery,
  deleteDeviceInfo,
  exportDeviceInfo,
  listDeviceInfo
} from '@/apis/task/deviceInfo'
import type { TableInstanceColumns } from '@/components/GiTable/type'

defineOptions({ name: 'DeviceInfo' })

const { destination, device_category, measure_unit } = useDict('destination', 'device_category', 'measure_unit')

const queryForm = reactive<DeviceInfoQuery>({
  name: undefined,
  category: undefined,
  modelNumber: undefined,
  factoryNumber: undefined,
  buyDate: undefined,
  sort: ['id,desc']
})

const {
  tableData: dataList,
  loading,
  pagination,
  search,
  handleDelete
} = useTable((page) => listDeviceInfo({ ...queryForm, ...page }), { immediate: true })
const columns: TableInstanceColumns[] = [
  { title: '设备名称', dataIndex: 'name', slotName: 'name' },
  { title: '设备类别', dataIndex: 'category', slotName: 'category' },
  { title: '管理编号', dataIndex: 'managementNumber', slotName: 'managementNumber' },
  { title: '型号', dataIndex: 'modelNumber', slotName: 'modelNumber' },
  { title: '出厂编号', dataIndex: 'factoryNumber', slotName: 'factoryNumber' },
  { title: '计量单位', dataIndex: 'measureUnit', slotName: 'measureUnit' },
  { title: '数量', dataIndex: 'count', slotName: 'count' },
  { title: '状态', dataIndex: 'status', slotName: 'status' },
  { title: '放置位置', dataIndex: 'location', slotName: 'location' },
  { title: '购置来源', dataIndex: 'source', slotName: 'source' },
  { title: '购置时间', dataIndex: 'buyDate', slotName: 'buyDate' },
  { title: '生产厂家', dataIndex: 'manufacturer', slotName: 'manufacturer' },
  { title: '购置金额', dataIndex: 'purchaseAmount', slotName: 'purchaseAmount' },
  { title: '备注', dataIndex: 'mark', slotName: 'mark' },
  { title: '物资去向', dataIndex: 'destination', slotName: 'destination' },
  {
    title: '操作',
    dataIndex: 'action',
    slotName: 'action',
    width: 160,
    align: 'center',
    fixed: !isMobile() ? 'right' : undefined,
    show: has.hasPermOr(['task:deviceInfo:detail', 'task:deviceInfo:update', 'task:deviceInfo:delete'])
  }
]

// 重置
const reset = () => {
  queryForm.name = undefined
  queryForm.category = undefined
  queryForm.modelNumber = undefined
  queryForm.factoryNumber = undefined
  queryForm.buyDate = undefined
  search()
}

// 删除
const onDelete = (record: DeviceInfoResp) => {
  return handleDelete(() => deleteDeviceInfo(record.id), {
    content: `是否确定删除该条数据？`,
    showModal: true
  })
}

// 导出
const onExport = () => {
  useDownload(() => exportDeviceInfo(queryForm))
}

const DeviceInfoAddModalRef = ref<InstanceType<typeof DeviceInfoAddModal>>()
// 新增
const onAdd = () => {
  DeviceInfoAddModalRef.value?.onAdd()
}

// 修改
const onUpdate = (record: DeviceInfoResp) => {
  DeviceInfoAddModalRef.value?.onUpdate(record.id)
}

const DeviceInfoDetailDrawerRef = ref<InstanceType<typeof DeviceInfoDetailDrawer>>()
// 详情
const onDetail = (record: DeviceInfoResp) => {
  DeviceInfoDetailDrawerRef.value?.onOpen(record.id)
}
</script>

<style scoped lang="scss"></style>
